package com.gadberry.utility.expression.function;

import java.util.List;

import com.gadberry.utility.algorithm.Algorithm;
import com.gadberry.utility.expression.Argument;
import com.gadberry.utility.expression.Expression;
import com.gadberry.utility.expression.Function;
import com.gadberry.utility.expression.InvalidArgumentsException;

/**
 * Converts each argument to a string then outputs the result to 
 * the console output. <br>
 * It adds a carriage return after the last argument so subsequent 
 * output will appear on a new line.
 * 
 * @author Peter Lager
 *
 */
public class PrintlnFunction extends Function{

	public PrintlnFunction(Expression expression) {
		super(expression);
	}

	@Override
	public Argument resolve(Algorithm algor) {
		List<Argument> args = getArguments();
		StringBuilder output = new StringBuilder();
		for (Argument arg : args) {
			output.append(arg.toString());
		}
		output.append(" ");
		System.out.println(output.toString());
		return new Argument(new Boolean(false), getResolver());
	}

	@Override
	protected void checkArgs(List<Argument> args)
			throws InvalidArgumentsException {
		
		for (Argument arg : args) {
			if(arg.isNull()) {
				throw new InvalidArgumentsException(
				"println cannot accept null arguments.  The argument provided is null.");
			} 
		}		
	}

}
